<?php
/******************************************************************************
* my little forum                                                             *
* Copyright (C) 2007 alex@mylittleforum.net                                   *
* http://mylittleforum.net/                                                   *
******************************************************************************/

/******************************************************************************
* This program is free software; you can redistribute it and/or               *
* modify it under the terms of the GNU General Public License                 *
* as published by the Free Software Foundation; either version 2              *
* of the License, or (at your option) any later version.                      *
*                                                                             *
* This program is distributed in the hope that it will be useful,             *
* but WITHOUT ANY WARRANTY; without even the implied warranty of              *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the                *
* GNU General Public License for more details.                                *
*                                                                             *
* You should have received a copy of the GNU General Public License           *
* along with this program; if not, write to the Free Software                 *
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. *
******************************************************************************/

define('IN_INDEX', TRUE);

session_start();

include('config/db_settings.php');
include('includes/functions.inc.php');
include('includes/main.inc.php');
require_once('uc_client/config.inc.php');
define('HTKOU_ROOT', dirname(__FILE__));

require 'modules/smarty/Smarty.class.php'; // requires plugin compiler.defun.php
$smarty = new Smarty;
$smarty->template_dir = 'templates';
$smarty->compile_dir = 'templates_c';
$smarty->config_dir = 'lang';
$smarty->config_overwrite = false;
$smarty->config_booleanize = false;
$smarty->config_load($settings['language_file']);
$lang = $smarty->get_config_vars();

ini_set('default_charset', $lang['charset']);
setlocale(LC_ALL, $lang['locale']);

$smarty->assign('settings', $settings);

if(isset($_SESSION[$settings['session_prefix'].'usersettings'])) $smarty->assign('usersettings', $_SESSION[$settings['session_prefix'].'usersettings']);
$smarty->assign('category', $category);
if(isset($categories)) $smarty->assign('categories', $categories);

$smarty->assign('total_postings', $total_postings);
$smarty->assign('total_spam', $total_spam);
$smarty->assign('total_threads', $total_threads);
$smarty->assign('registered_users', $registered_users);
if(isset($total_users_online))
 {
  $smarty->assign('total_users_online', $total_users_online);
  $smarty->assign('unregistered_users_online', $unregistered_users_online);
  $smarty->assign('registered_users_online', $registered_users_online);
 }
 
if(isset($_SESSION[$settings['session_prefix'].'user_id']) && isset($_SESSION[$settings['session_prefix'].'user_name']))
 {
  $smarty->assign('user_id', $_SESSION[$settings['session_prefix'].'user_id']);
  $smarty->assign('user', htmlspecialchars(stripslashes($_SESSION[$settings['session_prefix'].'user_name'])));
 }
if(isset($_SESSION[$settings['session_prefix'].'user_type']) && $_SESSION[$settings['session_prefix'].'user_type']==1) $smarty->assign('mod', true);
if(isset($_SESSION[$settings['session_prefix'].'user_type']) && $_SESSION[$settings['session_prefix'].'user_type']==2) $smarty->assign('admin', true);
if(isset($_SESSION[$settings['session_prefix'].'usersettings']['newtime'])) $smarty->assign('newtime', $_SESSION[$settings['session_prefix'].'usersettings']['newtime']);
if(isset($last_visit)) $smarty->assign('last_visit',$last_visit);
if(isset($menu)) $smarty->assign('menu',$menu);
if(isset($visited)) $smarty->assign('visited',$visited);

if(empty($mode) && isset($_REQUEST['mode']))
 {
  switch($_REQUEST['mode'])
   {
    case 'admin': $mode = 'admin'; break;
    #case 'board': $mode = 'board'; break;
    case 'board_entry': $mode = 'board_entry'; break;
    case 'contact': $mode = 'contact'; break;
    case 'delete_cookie': $mode = 'delete_cookie'; break;
    case 'login': $mode = 'login'; break;
    #case 'mix': $mode = 'mix'; break;
    case 'mix_entry': $mode = 'mix_entry'; break;
    case 'posting': $mode = 'posting'; break;
    case 'register': $mode = 'register'; break;
    case 'rss': $mode = 'rss'; break;
    case 'search': $mode = 'search'; break;
    case 'index': $mode = 'index'; break;
    case 'entry': $mode = 'entry'; break;
    case 'thread': $mode = 'thread'; break;
    case 'user': $mode = 'user'; break;
    case 'page': $mode = 'page'; break;
    case 'upload_image': $mode = 'upload_image'; break;
    case 'avatar': $mode = 'avatar'; break;
    case 'account_locked': $mode = 'account_locked'; break;
    case 'disabled': $mode = 'disabled'; break;
    default: $mode = 'index'; break;
   }
 }

if($settings['access_for_users_only'] == 1 && empty($_SESSION[$settings['session_prefix'].'user_id']))
 {
  if(empty($mode) || $mode!='account_locked' && $mode!='register' && $mode!='page') $mode = 'login';
 }
if($settings['forum_enabled']!=1 && (empty($_SESSION[$settings['session_prefix'].'user_type']) || $_SESSION[$settings['session_prefix'].'user_type']<2))
 {
  if(empty($mode) || $mode!='disabled' && $mode!='rss' && $mode!='login') $mode = 'disabled';
 }
if(empty($mode) && isset($_REQUEST['id'])) $mode = 'entry';

if(empty($mode)) 
 {
  // set user settings to default values if index page is requestes
  $_SESSION[$settings['session_prefix'].'usersettings']['thread_order']=1; //pasu 20080429 set default thread order to last_reply 
  $_SESSION[$settings['session_prefix'].'usersettings']['page']=1;
  $_SESSION[$settings['session_prefix'].'usersettings']['category']=0; 
  //pasu may 6. change default category back to 0 for new categories.
  //huofu Apr.29. change the default value from 0 to 2 so the default category now is set to shuigong
  $mode = 'index';
 } 

$smarty->assign('mode', $mode);

switch($mode)
 {
  case 'admin': include('includes/admin.inc.php'); break;
  case 'contact': include('includes/contact.inc.php'); break;
  case 'delete_cookie': include('includes/delete_cookie.inc.php'); break;
  case 'login': include('includes/login.inc.php'); break;
  case 'posting': include('includes/posting.inc.php'); break;
  case 'register': include('includes/register.inc.php'); break;
  case 'rss': include('includes/rss.inc.php'); break;
  case 'search': include('includes/search.inc.php'); break;
  case 'index': include('includes/index.inc.php'); break;
  case 'entry': include('includes/entry.inc.php'); break;
  case 'thread': include('includes/thread.inc.php'); break;
  case 'user': include('includes/user.inc.php'); break;
  case 'page': include('includes/page.inc.php'); break;
  case 'upload_image': include('includes/upload_image.inc.php'); break;
  case 'avatar': include('includes/avatar.inc.php'); break;          
  case 'account_locked': include('includes/account_locked.inc.php'); break;
  case 'disabled': include('includes/disabled.inc.php'); break;
 }

if(empty($template)) 
 {
  header('Location: index.php');
  exit;
 }

if($mode=='rss') 
 {
  header("Content-Type: text/xml; charset: ".$lang['charset']);
 } 
else 
 {
  #header('Cache-Control: public');
  header('Content-Type: text/html; charset='.$lang['charset']);
 }

$smarty->display($settings['template'].'/'.$template);
?>
